#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QMenuBar>
#include <QMessageBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QFontDialog>
#include <QColorDialog>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    /*
    QMenuBar *menubar = new QMenuBar(this);
    QMenu *menu1 = new QMenu("文件", this);
    QMenu *menu2 = new QMenu("编辑", this);

    menubar->addMenu(menu1);
    menubar->addMenu(menu2);

    QAction *action1 = new QAction("新建", this);
    menu1->addAction(action1);
    // connect(action1, &QAction::triggered, this, [this]()
    //         {
    //     QDialog dlg(this);
    //     dlg.setWindowTitle("新建");
    //     dlg.resize(200, 100);
    //     QVBoxLayout *layout = new QVBoxLayout;
    //     QLabel *label = new QLabel("请在此输入新建操作的说明文字", &dlg);
    //     QPushButton *btn = new QPushButton("确定", this);
    //     btn->setFixedSize(80, 30);

    //     layout->addWidget(label);
    //     layout->addWidget(btn, 0, Qt::AlignRight);
    //     dlg.setLayout(layout);
    //     connect(btn, &QPushButton::clicked, &dlg, &QDialog::close);
    //     dlg.exec(); });

    // connect(action1, &QAction::triggered, this, [this]()
    //         {
    //             QDialog *nonModalDialog = new QDialog(this);
    //             nonModalDialog->setWindowTitle("非模态对话框");
    //             nonModalDialog->resize(200, 100);
    //             QVBoxLayout *layout = new QVBoxLayout(nonModalDialog);
    //             QLabel *label = new QLabel("这是一个非模态对话框", nonModalDialog);
    //             nonModalDialog->setLayout(layout);
    //             QPushButton *btn = new QPushButton("确定", this);
    //             btn->setFixedSize(80, 30);

    //             layout->addWidget(label);
    //             layout->addWidget(btn, 0, Qt::AlignRight);
    //             nonModalDialog->setAttribute(Qt::WA_DeleteOnClose); // 确保对话框关闭时自动删除
    //             connect(btn, &QPushButton::clicked, nonModalDialog, &QDialog::close);
    //             nonModalDialog->show(); // 使用 show() 而不是 exec() 创建非模态对话框
    //          });

    connect(action1, &QAction::triggered, this, [this]()
            {
        QDialog *dlg = new QDialog(this);
        dlg->setAttribute(Qt::WA_DeleteOnClose);
        dlg->setModal(true);
        dlg->resize(200, 100);

        QVBoxLayout *layout = new QVBoxLayout(dlg);
        QLabel *label = new QLabel("这是一个非模态对话框", dlg);
        dlg->setLayout(layout);
        QPushButton *btn = new QPushButton("确定", this);
        btn->setFixedSize(80, 30);
        QPushButton *btn1 = new QPushButton("new", this);
        btn1->setFixedSize(80, 30);

        connect(btn, &QPushButton::clicked, dlg, &QDialog::close);
        connect(btn1, &QPushButton::clicked, this, [this](){
            QDialog *dlg1 = new QDialog(this);
            dlg1->setAttribute(Qt::WA_DeleteOnClose);
            dlg1->setModal(true);
            dlg1->resize(200, 100);
            dlg1->show();// 这个必须添加了才能显示对话框
        });

        layout->addWidget(label);
        layout->addWidget(btn, 0, Qt::AlignRight);
        layout->addWidget(btn1, 0, Qt::AlignLeft);

        dlg->show();
     });
    // Connect the '新建' action to show a modal dialog
    // connect(ui->actionNew, &QAction::triggered, this, &MainWindow::showNewDialog);
    */
    // QPushButton *btn = new QPushButton("消息对话框", this);
    // QMessageBox *msg = new QMessageBox(this);

    // // msg->setFixedSize(10, 150);
    // msg->setWindowTitle("Warning Message");
    // msg->setText("Error Message");
    /**
     * Question 正常操作过程中的提问
     * Infromation 正常信息
     * Warning 警告
     * Critical 致命错误
     */
    // msg->setIcon(QMessageBox::Critical);
    // msg->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    // connect(btn, &QPushButton::clicked, [msg]()
    //         { msg->show(); });
    // Colorfrank();
    // Filefrank();
    // Fontfrank();
    Inputfrank();
}

void MainWindow::showNewDialog()
{
    QMessageBox::information(this, "新建", "这是一个模态对话框");
    // QDialog dlg(this);
    // dlg.resize(200, 100);
    // dlg.exec();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Colorfrank()
{
    QPushButton *btn = new QPushButton("颜色对话框", this);
    QColorDialog *col = new QColorDialog(this);

    connect(btn, &QPushButton::clicked, [this, col]()
            {
        QColor _col = col->getColor(QColor(255, 0, 0));
        qDebug("r = %d", _col.red());
        qDebug("g = %d", _col.green());
        qDebug("b = %d", _col.blue()); });
}

void MainWindow::Fontfrank()
{
    QPushButton *btn = new QPushButton("字体对话框", this);
    connect(btn, &QPushButton::clicked, this, [this, btn]()
            {
        bool flag;
        QFont font = QFontDialog::getFont(&flag, QFont("华文楷体", 36));
        // 把char*转化为QString
        qDebug() << "字体: " << font.family().toUtf8().data();
        qDebug() << "字号: " << font.pointSize();
        qDebug() << "加粗: " << font.bold();
        qDebug() << "是否倾斜" << font.italic(); });
}

void MainWindow::Inputfrank()
{
    QPushButton *btn = new QPushButton("输入对话框", this);
    QInputDialog *input = new QInputDialog(this);

    // connect(btn, &QPushButton::clicked, this, [this, input]()
    //         {
    //     // 只能在整形范围内输入数字，超过范围的就不能输入了
    //     int d = input->getInt(this, "输入框", "整型");
    //     qDebug("d = %d", d); });
    connect(btn, &QPushButton::clicked, this, [this, input]()
            {
        QStringList items;
        items << tr("Spring") << tr("Summer") << tr("Autumer") << tr("Winter");
        QString item = input->getItem(this, "输入框", "Item", items);
        qDebug() << "item" << item.toUtf8().data(); });
}

void MainWindow::Filefrank()
{
    QPushButton *btn = new QPushButton("文件对话框", this);

    QFileDialog *dlg = new QFileDialog(this);
    connect(btn, &QPushButton::clicked, dlg, [this, dlg]()
            { QString file = dlg->getOpenFileName(this, "文件", "C:\\Users\\lenovo1\\Pictures", "*.png"); 
                qDebug("str: %s", file.toStdString()); });
}
